<template>
  <div class="main">
    <el-card class="box-card">
      <Form ref="form" :formobj="formobj" :formdata="formData" :formrules="formrules" />
      <div style="padding-left:100px">
        <el-button type="primary" @click="submit">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-card>
  </div>
</template>
<script>
import Form from '@/components/form.vue'
import { contentApiedit, contentApiqueryOne } from '@/api/webpage'
export default {
  name: 'Txldetails',
  components: {
    Form
  },
  data() {
    return {
      formData: {
        id: '',
        olumnId: '',
        title: '',
        content: '',
        fileUrl: '',
        listfile: []
      },
      lsfjlist: [],
      formobj: [
        { label: '标题', key: 'title', type: 'input' },
        { label: '内容', key: 'content', type: 'editor' },
        { label: '附件', key: 'listfile', type: 'execl', word: '请上传execl文件' }
      ],
      formrules: {
        title: [
          { required: true, message: '请输入标题', trigger: 'blur' }
        ],
        content: [
          { required: true, message: '请输入正文', trigger: 'change' }
        ]
      }
    }
  },
  created() {
    this.getdata('2293BE31C5B4449FA5487BA2228BA6ED')
  },
  methods: {
    getdata(id) {
      contentApiqueryOne({ id: id }).then(res => {
        console.log("获得数据：",res)
        if (res.code == 0) {
          const newsArray = []
          if(res.data.fileName){
            const arrayName = res.data.fileName.split(',')
            if(res.data.fileUrl){
              const array = res.data.fileUrl.split(',')
              array.forEach((item, index) => {
                const obj = {}
                obj.name = arrayName[index]
                obj.url = item
                newsArray.push(obj)
              })
            }
            console.log(newsArray)
          }
          this.formData = {
            id: res.data.id,
            columnId: res.data.columnId,
            title: res.data.title,
            content: res.data.content,
            listfile: newsArray,
            fileName: res.data.fileName
          }
        }
      })
    },
    submit() {
      this.$refs.form.getData().then(res => {
        console.log('🚀 ~ file: index.vue ~ line 69 ~ this.$refs.form.getData ~ res', res)
        // 处理fileName
        let b = []
        let a = []
        if(res.listfile && Array.isArray(res.listfile)){
          res.fileName = ''
          res.listfile.map(it=>{
            b.push(it.name)
            console.log("看看存在不存在show",it.url.indexOf('/show'))
            if(it.url.indexOf("/show") == -1){ // 不包含
              a.push(it.url)
            }else{
              a.push(it.url.substring(5)) // 截取/show 以后的字符串
            }
          })
          res.fileName = b.join(',')
          res.fileUrl = a.join(',')
        }
        console.log('🚀 ~ file: index.vue ~ line 86 ~ this.$refs.form.getData ~ res', res)
        contentApiedit(res).then((response) => {
          if (response.data == 1) {
            this.$message.success('成功')
            this.$router.go(-1)
          } else {
            this.$message.error('失败')
          }
        })
          .catch((err) => {
            console.log(err)
          })
      })
    },

    cancel() {
      this.$router.go(-1)
    }
  }
}
</script>
<style scoped>
@import "~@/assets/css/adminstyle.css";
</style>
